package com.nw.dsl4j.generator.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.nw.dsl4j.generator.DsljReflectionUtils;

public class Grammar {
	String name;
	String packageName;
	List<AntlrRule> rules = new ArrayList<AntlrRule>();
	List<AntlrRule> tokens = new ArrayList<AntlrRule>();
	List<AntlrRule> keywords = new ArrayList<AntlrRule>();
	int forwardLooking;
	
	public int getForwardLooking() {
		return forwardLooking;
	}
	public void setForwardLooking(int forwardLooking) {
		this.forwardLooking = forwardLooking;
	}
	public List<AntlrRule> getKeywords() {
		return keywords;
	}
	public void setKeywords(List<AntlrRule> keywords) {
		this.keywords = keywords;
	}
	public List<AntlrRule> getRules() {
		return rules;
	}
	public void setRules(List<AntlrRule> rules) {
		this.rules = rules;
	}
	public List<AntlrRule> getTokens() {
		return tokens;
	}
	public void setTokens(List<AntlrRule> tokens) {
		this.tokens = tokens;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		String ret = StringUtils.join(keywords.toArray(), "\n");
		ret += "\n" + StringUtils.join(tokens.toArray(), "\n");
		ret += "\n" + StringUtils.join(rules.toArray(), "\n");
		return ret;
	}
	public boolean ruleAlreadyExist(Class<?> cls) {
		String antlrRuleName = DsljReflectionUtils.getAntlrRuleName(cls);
		for (AntlrRule rule : getRules()) {
			if (rule.getName().equals(antlrRuleName)) {
				return true;
			}
		}
		for (AntlrRule rule : getTokens()) {
			if (rule.getName().equals(antlrRuleName)) {
				return true;
			}
		}
		for (AntlrRule rule : getKeywords()) {
			if (rule.getName().equals(antlrRuleName)) {
				return true;
			}
		}
		return false;
	}
	
	public String getPackageName() {
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

}
